var redis = require("redis");
var config = require('../config');
var logger = require('../logger');
var db = require('./db');

var _client = null;

function log() {
    logger.log(arguments);
}

function connect(cb) {
    //创建redis客户端
    _client = redis.createClient(config.redisPort, config.redisHost);
    //连接错误处理
    _client.on("error", (err) => {
        if (err) {
            cb('redis connect err:' + err);
        }
    });
    _client.select(config.redisMusicDatabase, (err) => {
        if (err) {
            cb('redis select database:' + err);
        } else {
            cb(null);
        }
    })
}

function client() {
    return _client;
}

function addtag(fileid, maintag, childtag, grandsontag, func) {
    db.addtag(_client,fileid,maintag,childtag,grandsontag,func);
}

function getfileinfo(fileid, cb) {
    db.getfileinfo(_client,fileid,cb);
}

function readfile(resourcepath,fileid, cb) {
    db.readfile(_client,resourcepath,fileid,cb);
}

function getallmaitags(cb) {
    db.getallmaitags(_client,cb);
}

function getfiletags(fileid, cb) {
    db.getfiletags(_client,fileid,cb);
}

function setfileinfo(fileid, info, cb) {
    db.setfileinfo(_client,fileid,info,cb);
}

function gettagtree(cb) {
    db.gettagtree(_client,cb);
}

module.exports = {
    addtag,
    connect,
    client,
    getfileinfo,
    readfile,
    getallmaitags,
    getfiletags,
    setfileinfo,
    gettagtree,
}